package com.ujs.controller;

import com.ujs.pojo.Order;
import com.ujs.pojo.Product;
import com.ujs.service.ProductService;
import com.ujs.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @title: ProductController
 * @Author liyunxiao
 * @Date: 2021/1/10 15:11
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


//    @GetMapping("/findAll")
    public String findAll(Model model){
        List<Product> products = productService.findAll();
        model.addAttribute("products", products);
        return "product-list";
    }

    @RequestMapping("/findAll")
    public String findAllByPage(Model model,
                                @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<Product> list = productService.findAll();
        PageUtil<Product> pageInfo = new PageUtil<>(page, pageSize, list);
        List<Product> products = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("products", products);
        return "product-list";
    }

    @GetMapping("/findByProductNameLike")
    public String findByProductNameLike(Model model,String name,
                                        @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                        @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<Product> list = productService.findByProductNameLike(name);
        PageUtil<Product> pageInfo = new PageUtil<>(page, pageSize, list);
        List<Product> products = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("products", products);
        return "product-list";
    }
    @RequestMapping("/save")
    public String save(Model model,  Product product){
        System.out.println(product);
        productService.save(product);
        return "redirect:findAll";
    }

    @GetMapping("/product-add")
    public String add(Model model ){
        return "product-add";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Model model,String id){
        System.out.println(id);
        productService.deleteById(id);

        return "redirect:findAll";
    }




}
